package com.lzw.service.impl;

import com.lzw.domain.dto.SQLFormat2;
import com.lzw.domain.vo.ResponseVO;
import com.lzw.mapper.FacilityMapper;
import com.lzw.service.Line5Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

@Service
public class Line5ServiceImpl implements Line5Service {

    @Autowired
    private FacilityMapper facilityMapper;

    /**
     * 生产数量
     */
    private Integer productionNum;

    /**
     * 合格数量
     */
    private Integer qualifiedNum;

    /**
     * 不合格数量
     */
    private Integer unqualifiedNum;

    /**
     * 合格率
     */
    private Double percentOfPass = 0.0;

    /**
     * 日期
     */
    private String t;

    @Override
    public List<ResponseVO> line5GetMsg(String procedureName) {
        List<SQLFormat2> lists = facilityMapper.get15dayMsg(procedureName);
        ResponseVO responseVO;
        List<ResponseVO> responseVOList = new ArrayList<>();
        for (SQLFormat2 list :
                lists) {
            productionNum = Integer.parseInt(list.getProductNumber_Total());
            unqualifiedNum = Integer.parseInt(list.getProductNumber_NG());
            qualifiedNum = productionNum - unqualifiedNum;
            //合格率=合格数量/生产数量，保留两位小数
            BigDecimal bigDecimal1 = BigDecimal.valueOf(qualifiedNum * 100);
            BigDecimal bigDecimal2 = BigDecimal.valueOf(productionNum);
            BigDecimal divide = bigDecimal1.divide(bigDecimal2, 2, RoundingMode.HALF_UP);
            percentOfPass = divide.doubleValue();
            responseVO = new ResponseVO(list.getDate(), list.getUnitName(), productionNum, qualifiedNum, unqualifiedNum, percentOfPass);
            responseVOList.add(responseVO);
        }
        return responseVOList;
    }
}
